﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rabbit.Logic;
using RabbitShop.Code;

namespace RabbitShop.Action
{
    public partial class CartItemDelete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid = string.Empty;
            if (Request.Cookies[Constant.CookieName] != null)
            {
                uid = Request.Cookies[Constant.CookieName].Values[Constant.CookieUid];
                if (string.IsNullOrEmpty(uid)) Response.Redirect("~/UI/UserLogon.aspx");
            }
            else
            {
                Response.Redirect("~/UI/UserLogon.aspx");
            }

            string oid = Request["oid"];
            if (string.IsNullOrEmpty(oid)) return;

            Customer customer = RabbitUse.GetCustomerById(Db.Store, uid);
            if (customer.Cart == null)
            {
                customer.Cart = new List<ShopCartItem>();
            }

            for (int i = 0; i < customer.Cart.Count; i++)
            {
                if (customer.Cart[i].ProductId == oid)
                {
                    customer.Cart.RemoveAt(i);
                    break;
                }
            }

            RabbitUse.AddOrUpdateCustomer(Db.Store, customer);

            Logic.UpdateCookieCartNo(this, customer.Cart.Count.ToString());

            Response.Redirect("~/UI/CartView.aspx");
        }
    }
}